package com.cems.controller.forum;

import com.cems.pojo.*;
import com.cems.service.ForumService;
import com.cems.service.UserService;
import com.cems.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @ClassName ArticleController
 * @Author 陈新予(blank)
 * @Date 2021/5/25
 * @Version 1.0
 */
@Controller
public class ArticleController {
    @Autowired
    ForumService forumService;
    @Autowired
    UserService userService;

    @GetMapping("toArticleInfo")
    public String toArticleInfo(int id, Model model, HttpSession session) {
        //获取登录的用户的数据
        try {
            SysUser user = (SysUser) session.getAttribute("user");
            model.addAttribute("loginUserPname", userService.selOneUser(user.getId()).getUserPname());
            model.addAttribute("myId", user.getId());
        } catch (Exception e) {
            return "User/userlogin";
        }
        //查询帖子
        ForumArticle article = forumService.getArticleById(id);
        //查询评论
        List<ForumCommentBig> commList = forumService.getForumCommentListByArt(id);
        model.addAttribute("art_title", article.getFoTitle());
        model.addAttribute("art_id", article.getId());
        model.addAttribute("art_Data", article.getFoDate());
        model.addAttribute("art_time", article.getGmtCreate());
        model.addAttribute("userPname", userService.selOneUser(article.getUserId()).getUserPname());
        model.addAttribute("userId", article.getUserId());
        model.addAttribute("userPnameOne", userService.selOneUser(article.getUserId()).getUserPname().substring(0, 1));
        List<String> userPname = new ArrayList<>();
        List<Integer> userlistId = new ArrayList<>();
        List<String> timeList = new ArrayList<>();
        List<String> comData = new ArrayList<>();
        List<Integer> numlist = new ArrayList<>();
        List<Integer> comIds = new ArrayList<>();
        List<List<List<String>>> replies = new ArrayList<>();
        int num = 0;
        for (ForumCommentBig com : commList) {
            userlistId.add(com.getUserId());
            userPname.add(com.getUserPname());
            timeList.add(new DateUtil().dateToString(com.getBigGmtCreate()));
            comData.add(com.getCommData());
            numlist.add(num);
            comIds.add(com.getId());
            num++;
            List<List<String>> cplist = new ArrayList<>();
            for (CommentReply reply : com.getReplies()) {
                List<String> cp = new ArrayList<>();
                cp.add(String.valueOf(reply.getId()));
                cp.add(reply.getCommReData());
                cp.add(userService.selOneUser(reply.getCommReUser()).getUserPname());
                cp.add(userService.selOneUser(reply.getCommUser()).getUserPname());
                cp.add(new DateUtil().dateToString(reply.getGmtCreate()));
                cp.add(String.valueOf(reply.getCommUser()));
                cplist.add(cp);
            }
            replies.add(cplist);
        }
        int pages;
        if (commList.size() % 3 == 0) {
            pages = commList.size() / 3;
        } else {
            pages = commList.size() / 3 + 1;
        }
        model.addAttribute("userlist", userPname);
        model.addAttribute("userlistId", userlistId);
        model.addAttribute("comIds", comIds);
        model.addAttribute("timeList", timeList);
        model.addAttribute("comData", comData);
        model.addAttribute("replies", replies);
        model.addAttribute("pages", pages);
        model.addAttribute("numlist", numlist);
        return "font/article/articleInfo";
    }

    @GetMapping("addComment")
    @ResponseBody
    public String addComment(String dec, int artId, HttpSession session) {
        //添加评论
        HashMap<String, String> map = new HashMap<>();
        try {
            SysUser user = (SysUser) session.getAttribute("user");
            map.put("userId", String.valueOf(user.getId()));
        } catch (Exception e) {
            return "3,-1";
        }
        try {
            map.put("artid", String.valueOf(artId));
            //获取登录用户id
            map.put("data", dec);
            map.put("commentId", String.valueOf(123));
            int commId = forumService.addComment(map);
            return "1," + commId;
        } catch (Exception e) {
            e.printStackTrace();
            return "0,-1";
        }
    }

    @GetMapping("addReply")
    @ResponseBody
    public int addReply(int toUser, int commId, String data, int artId, HttpSession session) {
        HashMap<String, String> map = new HashMap<>();
        try {
            SysUser user = (SysUser) session.getAttribute("user");
            map.put("userId", String.valueOf(user.getId()));
        } catch (Exception e) {
            return 3;
        }
        try {
            //添加回复
            map.put("data", data);
            map.put("reUserId", String.valueOf(toUser));
            map.put("commId", String.valueOf(commId));
            forumService.addReply(map, artId);
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
}
